package com.shuzijun.leetcode.editor.cn;
public class InsertIntoBitsLcci{
    public static void main(String[] args) {
         Solution solution = new InsertIntoBitsLcci().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int insertBits(int N, int M, int i, int j) {
        int[] nbits = new int[32];
        int[] mbits = new int[32];
        int nbitsNum = 0;
        int mbitsNum = 0;
        int mask = 1;
        for (int k = 0; k < 32; k++) {
            if ((N&mask) != 0){
                nbits[k] = 1;
            }
            mask <<= 1;
        }

        mask = 1;
        for (int k = 0; k < 32; k++) {
            if ((M&mask) != 0){
                mbits[k] = 1;
            }
            mask <<= 1;
        }

        for (int k = i; k <= j; k++) {
            nbits[k] = mbits[k-i];
        }

        mask = 1;
        int ret = 0;
        for (int k = 0; k < 32; k++) {
            ret += (nbits[k] * mask);
            mask <<= 1;
        }
        return ret;

    }
}
//leetcode submit region end(Prohibit modification and deletion)

}